#include <stdio.h>
#include <stdlib.h>
#include <string.h>


#include "esp_system.h"
#include "nvs_flash.h"
#include "nvs.h"


#include "cleaner.h"
#include "myuart.h"


cleaner* get_cleaner(void){
    cleaner* pCleaner = (cleaner*)malloc(sizeof(cleaner));
    pCleaner->Battery.CurrentVal = BAT_VAL_DEFAULT;
    pCleaner->Battery.ChargeState = CHA_DEFAULT;

    pCleaner->Work.CarStartTime = 1800;
    pCleaner->Work.TargetStartTime = 1800;
    pCleaner->Work.WorkState = STA_DEFAULT;
    workplace defaultWorkPlace[WORKPLACE_LEN] = {'L','1','-','2','0','5'};
    strcpy(pCleaner->Work.WorkPlace, defaultWorkPlace);
    return pCleaner;
}

esp_err_t store_cleanerConfig_to_nvs(cleaner* pCleanerCar){
    nvs_handle carNvsHandle;
    esp_err_t err;

    err = nvs_open("car_config", NVS_READWRITE, &carNvsHandle);
    if (err != ESP_OK) {
        printf("Error (%s) opening NVS carNvsHandle!\n", esp_err_to_name(err));
        return err;
    }
    
    //储存工作地点
    nvs_set_str(carNvsHandle, WORK_PLACE_KEY, (const char *)pCleanerCar->Work.WorkPlace);
    //储存目标开始工作时间
    nvs_set_u16(carNvsHandle, WORK_STARTTIME_KEY, pCleanerCar->Work.TargetStartTime);

    nvs_commit(carNvsHandle);
    nvs_close(carNvsHandle);
    return ESP_OK;
}


esp_err_t store_cleaner_place_to_nvs(cleaner* pCleanerCar){
    nvs_handle carNvsHandle;
    esp_err_t err;

    err = nvs_open("car_config", NVS_READWRITE, &carNvsHandle);
    if (err != ESP_OK) {
        printf("Error (%s) opening NVS carNvsHandle!\n", esp_err_to_name(err));
        return err;
    }
    
    //储存工作地点
    nvs_set_str(carNvsHandle, WORK_PLACE_KEY, (const char *)pCleanerCar->Work.WorkPlace);

    nvs_commit(carNvsHandle);
    nvs_close(carNvsHandle);
    return ESP_OK;
}

esp_err_t store_cleaner_time_to_nvs(cleaner* pCleanerCar){
    nvs_handle carNvsHandle;
    esp_err_t err;

    err = nvs_open("car_config", NVS_READWRITE, &carNvsHandle);
    if (err != ESP_OK) {
        printf("Error (%s) opening NVS carNvsHandle!\n", esp_err_to_name(err));
        return err;
    }
    //储存目标开始工作时间
    nvs_set_u16(carNvsHandle, WORK_STARTTIME_KEY, pCleanerCar->Work.TargetStartTime);
    
    nvs_commit(carNvsHandle);
    nvs_close(carNvsHandle);
    return ESP_OK;
}




esp_err_t get_cleanerConfig_from_nvs(cleaner* pCleanerCar){
    nvs_handle carNvsHandle;
    esp_err_t err;

    err = nvs_open("car_config", NVS_READWRITE, &carNvsHandle);
    if (err != ESP_OK) {
        printf("Error (%s) opening NVS carNvsHandle!\n", esp_err_to_name(err));
        return err;
    }

    //获取地点
    uint8_t place[WORKPLACE_LEN] = {0};
    uint8_t place_len = WORKPLACE_LEN;
    err = nvs_get_str(carNvsHandle, WORK_PLACE_KEY, (char*)place, &place_len);
    if (err != ESP_OK) {
        printf("get place failed, err(%s)\n", esp_err_to_name(err));
        return err;
    }
    printf("get place = %s \n",place);
    strcpy(pCleanerCar->Work.WorkPlace, (const char *)place);

    //获取目标工作时间
    uint16_t time = 0;
    err = nvs_get_u16(carNvsHandle, WORK_STARTTIME_KEY, &time);
    if (err != ESP_OK) {
        printf("get start time failed, err(%s)\n", esp_err_to_name(err));
        return err;
    }
    printf("get target time = %d \n",time); 
    pCleanerCar->Work.TargetStartTime = time;
    
    nvs_commit(carNvsHandle);
    nvs_close(carNvsHandle);
    return ESP_OK;
}  

// }